package priv.pront.code.structure.linkedList;

import java.util.Stack;

public class IsPalindromeList {
    public static class Node{
        public int value;
        public Node next;

        public Node(int value) {
            this.value = value;
        }
    }
    //    need n extra space
    public static boolean IsPalindromeList1(Node head) {
        if (head == null || head.next == null) {
            return true;
        }
        Stack<Node> stack = new Stack<>();
        Node cur = head;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        while (head != null) {
            if (stack.pop().value != head.value) {
                return false;
            }
            head = head.next;
        }
        return true;
    }

//    need n/2 extra space (double pointer)
    public static boolean IsPalindromeList2(Node head) {
        if (head == null || head.next == null) {
            return true;
        }
        Node right = head.next;
        Node cur = head;
        while (cur.next != null && cur.next.next != null) {
//           at least 3
            right = right.next;
            cur = cur.next.next;
        }
        Stack<Node> stack = new Stack<>();
        while (right != null) {
            stack.push(right);
            right = right.next;
        }
        while (!stack.isEmpty()) {
            if (head.value != stack.pop().value) {
                return false;
            }
            head = head.next;
        }
        return true;
    }

//    need O(1) extra space
    public static boolean IsPalindromeList3(Node head) {
        if (head == null || head.next == null) {
            return true;
        }
        Node n1 = head;
        Node n2 = head;
        while (n2.next != null && n2.next.next != null) {
            n1 = n1.next;
            n2 = n2.next.next;
        }
        n2 = n1.next; //right part first node
        n1.next = null; //mid.next =null
        Node n3 = null;
        while (n2 != null) { //right part reverse
            n3 = n2.next;  //save next node
            n2.next = n1;  //next of right node convert
            n1 = n2;  //n1 move
            n2 = n3;  //n2 move
        }
        n3 = n1;
        n2 = head;
        boolean res = true;
        while (n1 != null & n2 != null) {  //check palindrome
            if (n1.value != n2.value) {
                res = false;
                break;
            }
            n1 = n1.next;  //left to mid
            n2 = n2.next;  //right to mid
        }
        n1 = n3.next;
        n3.next = null;
        while (n1 != null) {
            n2 = n1.next;
            n1.next = n3;
            n3 = n1;
            n1 = n2;
        }
        return res;
    }
//    for test
    public static void main(String[] args) {
        Node head = new Node(5);
        head.next = new Node(4);
        head.next.next = new Node(2);
        head.next.next.next = new Node(3);
        head.next.next.next.next = new Node(2);
        head.next.next.next.next.next = new Node(4);
        head.next.next.next.next.next.next = new Node(5);
//        System.out.println(IsPalindromeList1(head));
//        System.out.println(IsPalindromeList2(head));
        System.out.println(IsPalindromeList3(head));
    }
}
